package com.uniview.web.contorller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.uniview.base.BaseController;
import com.uniview.entity.po.Employees;
import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.ResultMessage;
import com.uniview.entity.vo.TableJsonVO;
import com.uniview.service.EmployeesService;

@RestController
@RequestMapping(value = "/sys")
public class EmployeesController extends BaseController {
	Logger logger = Logger.getLogger(EmployeesController.class);

	@Autowired
	private EmployeesService employeesService;

	@RequestMapping(value = "/findAllEmployees")
	@ResponseBody
	public TableJsonVO findAllEmployees(HttpServletRequest request){
		try{
			int page = ServletRequestUtils.getIntParameter(request, "page", 0);
			int limit = ServletRequestUtils.getIntParameter(request, "limit", 20);
			String empCode = ServletRequestUtils.getStringParameter(request, "empCode");
			String dept = ServletRequestUtils.getStringParameter(request, "deptName");
			Pager<Employees> pager = new Pager<>();
			pager = employeesService.findAllEmployeesByPage(empCode, dept, page, limit);
			
			if (pager != null && pager.getPagerList() != null && pager.getPagerList().size() > 0) {
//				return getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()), pager.getTotalRow());
				return getTableJsonVO(pager.getPagerList(), pager.getTotalRow());
			}
		}catch(Exception e){
			logger.error("获取员工信息异常",e);
		}
		
//		return getTableJsonString("[]", 0);
		return getTableJsonVO(new ArrayList<Employees>(), 0);
	}

	
	@RequestMapping(value = "/deleteEmployees")
	@ResponseBody
	public ResultMessage deleteEmployees(HttpServletRequest request){
		boolean result = false;
		String msg = "删除成功！";
		try{
			int emId = ServletRequestUtils.getIntParameter(request, "emId", -1);
			result = employeesService.deleteEmployeesById(emId);
			if(!result){
				msg = "删除失败";
			}
		}catch(Exception e){
			msg = "删除员工信息异常";
			logger.error(msg,e);
		}
		return getResult(msg, result);
	}
}
